import { ref, onMounted } from "vue";
import { gasSpotCheckRecordCreateAPI, gasSpotCheckRecordQueryDetailAPI, gasSpotCheckRecordHistoryAPI } from "@/api/consumption/common";
import dayjs from "dayjs";
import { PxMessage } from "px-ui";
// val string
// val:BULK_GAS,SPECIALTY_GAS,CHEMICAL
export const useSpotCheckHook = val => {
  const spotCheckHistorykData = ref([]);
  const spotCheckDialogVisible = ref(false);
  const spotCheckDisabled = ref(false);

  onMounted(() => {
    spotCheckDetail();
  });

  // 点检详情
  const spotCheckDetail = async () => {
    try {
      const paramsDetail = {
        itemType: val,
        itemDate: dayjs().format("YYYY-MM-DD")
      };
      const resData = await gasSpotCheckRecordQueryDetailAPI(paramsDetail);
      // 点检过
      if (resData?.checkStatus === 1) {
        spotCheckDisabled.value = true;
      }
      // 点检过
    } catch (error) {}
  };

  // 点检按钮点击事件
  const spotCheckClick = async () => {
    // 值班工程师：点击后变灰，第二天恢复可操作状态，
    // 系统工程师：点击后，弹出点检记录
    try {
      const paramsCreate = {
        itemType: val,
        itemDate: dayjs().format("YYYY-MM-DD"),
        checkStatus: 0
      };
      const res = await gasSpotCheckRecordCreateAPI(paramsCreate);
      if (res) {
        PxMessage.success("点检成功");
      }
    } catch (error) {}
  };

  // 点检历史的数据
  const spotCheckHistory = async () => {
    try {
      const res = await gasSpotCheckRecordHistoryAPI({ itemType: val });
      if (res?.length) {
        res.forEach(item => {
          item.name = item.operator + "-" + item.operatorName;
        });
        spotCheckHistorykData.value = res;
      } else {
        spotCheckHistorykData.value = [];
      }

      // spotCheckHistorykData.value
    } catch (error) {}
  };

  // 点检历史按钮点击事件
  const spotCheckClickHistory = () => {
    spotCheckDialogVisible.value = true;
    spotCheckHistory();
  };

  return {
    spotCheckHistorykData,
    spotCheckDialogVisible,
    spotCheckDisabled,
    spotCheckClick,
    spotCheckClickHistory
  };
};

// val
// SPECIALTY_GAS => specialGas, 特气
// BULK_GAS => bulkGas, 大宗气
// CHEMICAL => chemical,化学品
// DETAIL => detail, 明细
// METERREADING  => meterReading 抄表修正
export const useExportHook = (proxy, val, params) => {
  let prefix = "/operation/api/v1/gas";
  const obj = {
    SPECIALTY_GAS: `${prefix}/specialtyGas/queryConsumptionDetailExport`,
    SPECIALTY_GAS_METERREADING: `${prefix}/specialtyGas/queryReadValueListExport`,
    BULK_GAS: `${prefix}/bulkGas/queryFlowmeterExport`,
    CHEMICAL: `${prefix}/chemical/queryConsumptionDetailExport`,
    CHEMICAL_METERREADING: `${prefix}/chemical/queryReadValueListExport`
  };

  const nameObj = {
    SPECIALTY_GAS: "特气耗量明细.xlsx",
    SPECIALTY_GAS_METERREADING: "特气自动抄表修正.xlsx",
    BULK_GAS: "大宗气流量计.xlsx",
    CHEMICAL: "化学品耗量明细.xlsx",
    CHEMICAL_METERREADING: "化学品自动抄表修正.xlsx"
  };
  const handleExport = () => {
    proxy.download("post", obj[val], params, nameObj[val]);
  };

  return {
    handleExport
  };
};
